﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;

using System.Windows.Controls;

using TJDevHouse.Starcraft.Game;

namespace TJDevHouse.Starcraft.MapAnalyser.MapDrawing
{
	public class WalkabiltiyZonesMapLayer : MapLayer
	{
		private Dictionary<int, Brush> _zoneBrushes = new Dictionary<int,Brush>();

		private Random rand = new Random();

		private int _mostCommonZone;

		private Brush _mostCommonBrush;

		protected override void RenderLayer(MapDisplay display)
		{

			Analysers.WalkabilityAnalyser analyser = MapAnalysisController.GetMapAnalysisController(display.Map).WalkabilityAnalyser;

			analyser.LoadOrDoAnalysis();

			DoZoneCount(analyser);


			Map map = display.Map;

			if (map == null)
			{
				return;
			}

			Rectangle currentRect = new Rectangle();
			int width = 0;
			bool doRender = true;

			int prevZone = -1;
			for(int y = 0; y < map.MapWalkableHeight; y++)
			{
				prevZone = -1;
				for (int x = 0; x < map.MapWalkableWidth; x++)
				{

					int zone = analyser.Result.ZonesByTiles[y, x];


					if (zone == prevZone)
					{
						width++;
					}
					else
					{
						if (width > 0)
						{
							if (doRender)
							{
								width++;
								currentRect.Width = width * display.WalkTileSize;

								LayerCanvas.Children.Add(currentRect);
							}
						}

						if (zone == _mostCommonZone)
						{
							if (_mostCommonBrush == null)
							{
								_mostCommonBrush = GetZoneColor(zone);
							}
							doRender = false;
						}
						else
						{
							doRender = true;

							Brush brush = GetZoneColor(zone);
							currentRect = new Rectangle();
							currentRect.Fill = brush;
							currentRect.Stroke = brush;
							currentRect.Height = display.WalkTileSize;
							display.PositionElementByWalkTile(currentRect, x, y);
							width = 1;

						}
					}

					prevZone = zone;
				}

			}

			LayerCanvas.Background = _mostCommonBrush;
			Console.WriteLine("Processed, now rendering...");
		}

		public void DoZoneCount(Analysers.WalkabilityAnalyser analyser)
		{
			int maxZone = -1;
			int maxZoneCount = 0;
			foreach (KeyValuePair<int, List<WalkTile>> zoneKVP in analyser.Result.TilesByZone)
			{
				if (zoneKVP.Value.Count > maxZoneCount)
				{
					maxZoneCount = zoneKVP.Value.Count;
					maxZone = zoneKVP.Key;
				}
			}

			_mostCommonZone = maxZone;
		}

		public Brush GetZoneColor(int zoneID)
		{
			if (!_zoneBrushes.ContainsKey(zoneID))
			{
				if (zoneID == 0)
				{
					_zoneBrushes[0] = new SolidColorBrush(Colors.Black);
				}
				else
				{
					Console.WriteLine("Creating a brush for zone " + zoneID);
					_zoneBrushes[zoneID] = new SolidColorBrush(Color.FromRgb((byte)rand.Next(0, 255), (byte)rand.Next(0, 255), (byte)rand.Next(0, 255)));
				}
				_zoneBrushes[zoneID].Freeze();
			}
			return _zoneBrushes[zoneID];
		}
	}
}
